package com.jrx.anytxn.reporting.job.xib.config;

import com.jrx.anytxn.customer.entity.CmCustomerFirstLevel;
import com.jrx.anytxn.param.mapper.ext.ExtBtBatchStatusMapper;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.reporting.config.AnyTxnBatchProperties;
import com.jrx.anytxn.reporting.entity.CmInteCoCustInfo;
import com.jrx.anytxn.reporting.job.xib.step.CustInfoReportingListener;
import com.jrx.anytxn.reporting.job.xib.step.CustInfoReportingProcesser;
import com.jrx.anytxn.reporting.job.xib.step.CustInfoReportingWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;

/**
 * @comment: 零售客户信息报表任务定义
 * @author: hsoong
 * @date: 2021/7/1 10:53
 */
@Configuration
@EnableConfigurationProperties(AnyTxnBatchProperties.class)
@EnableBatchProcessing
public class CustInfoReportingConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    private AnyTxnBatchProperties anyTxnBatchProperties;

    @Resource
    private ExtBtBatchStatusMapper extBtBatchStatusMapper;

    @Autowired
    private IOrganizationTableService organizationTableService;

    @Autowired
    CustInfoReportingListener custInfoReportingListener;

    @Autowired
    CustInfoReportingProcesser custInfoReportingProcesser;

    @Autowired
    CustInfoReportingWriter custInfoReportingWriter;


    @Bean
    public Job xibCustInfoReportingJob(){
        return jobBuilderFactory.get("xibCustInfoReportingJob")
                .incrementer(new RunIdIncrementer())
                .listener(custInfoReportingListener)
                .start(xibCustInfoReportingJobStep())
                .build();
    }

    @Bean
    public Step xibCustInfoReportingJobStep(){
        return stepBuilderFactory.get("xibCustInfoReportingJobStep")
                .<CmCustomerFirstLevel, CmInteCoCustInfo>chunk(anyTxnBatchProperties.getReportingCustomerConfig().getChunkLimit())
                .reader(custInfoReportingReader())
                .processor(custInfoReportingProcesser)
                .writer(custInfoReportingWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getReportingCustomerConfig().getThrottleLimit())
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<CmCustomerFirstLevel> custInfoReportingReader() {
        MyBatisCursorItemReader<CmCustomerFirstLevel> cursorItemReader = new MyBatisCursorItemReader<>();
        cursorItemReader.setSaveState(false);
        cursorItemReader.setSqlSessionFactory(sqlSessionFactory);
        cursorItemReader.setQueryId("com.jrx.anytxn.customer.mapper.ext.ExtCmCustomerFirstLevelMapper.selectAllCust");
        SynchronizedItemStreamReader<CmCustomerFirstLevel> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(cursorItemReader);
        return synchronizedItemStreamReader;
    }

}
